package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.pojo.User;
import com.itheima.commons.pojo.UserInfo;
import com.itheima.dubbo.interfaces.UserApi;
import com.itheima.dubbo.interfaces.UserInfoApi;
import com.itheima.dubbo.mysql.mapper.UserInfoMapper;
import com.itheima.dubbo.mysql.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class UserInfoImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public void addUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void updatePic(Long uid, String picUrl) {
        userInfoMapper.update(UserInfo.builder()
                .logo(picUrl)
                .build(), Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserId, uid));
    }

    //根据id查询用户信息
    @Override
    public UserInfo findById(Long userId) {
        UserInfo userInfo = userInfoMapper.selectOne(
                Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserId, userId));
        return userInfo;
    }

    //根据userInfo的条件，以及用户ids查询用户信息
    @Override
    public Map<Long, UserInfo> findUserInfoByUserid(UserInfo userInfo, List<Long> userIdList) {
        List<UserInfo> userInfos = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getUserId, userIdList)
                //.eq(UserInfo::getSex, userInfo.getSex())
                //.eq(UserInfo::getCity, userInfo.getCity())
                //.le(UserInfo::getAge, userInfo.getAge())
        );
        Map<Long, UserInfo> userId = CollUtil.fieldValueMap(userInfos, "userId");
        return userId;
    }
}
